package org.csu.mypetstore.controller;

import com.sun.org.apache.xpath.internal.operations.Or;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.LineItemMapper;
import org.csu.mypetstore.persistence.OrderMapper;
import org.csu.mypetstore.persistence.SequenceMapper;
import org.csu.mypetstore.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    SequenceMapper sequenceMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    LineItemMapper lineItemMapper;

    @Autowired
    AdminService adminService;

    //点进procedure to check in
    @GetMapping("/newOrder")
    public String newOrder(HttpServletRequest req){
        HttpSession session = req.getSession();

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date createDate = new Date(System.currentTimeMillis());
        session.setAttribute("createDate",formatter.format(createDate));

        return "order/NewOrder";
    }

    //下一层
    @PostMapping("/confirmOrder")
    public String confirmOrder(HttpServletRequest req){
        HttpSession session = req.getSession();

        String cardType = req.getParameter("cardType");
        String creditCard = req.getParameter("creditCard");
        String expiryDate = req.getParameter("expiryDate");

        Account account = (Account)session.getAttribute("account");
        Cart cart = (Cart)session.getAttribute("cart");

        Order order = new Order();
        order.initOrder(account,cart);

        session.setAttribute("cardType",cardType);
        session.setAttribute("creditCard",creditCard);
        session.setAttribute("expiryDate",expiryDate);
        session.setAttribute("order",order);

        return "order/ConfirmOrder";
    }

    //第三层
    @GetMapping("/viewOrder")
    public String viewOrder(HttpServletRequest req){
        HttpSession session = req.getSession();
        Order order = (Order)session.getAttribute("order");

        Account account = (Account) session.getAttribute("account");
        Cart cart = (Cart)session.getAttribute("cart");
        String creditCard = (String) session.getAttribute("creditCard");
        String cardType = (String)session.getAttribute("cardType");

        //得到并更新序号
        Sequence sequence = new Sequence();
        sequence.setName("ordernum");

        int orderId = adminService.updateSequenceGetOrderId(sequence);

        order.setOrderId(orderId);
        order.setCreditCard(creditCard);
        order.setCardType(cardType);
        order.initOrder(account,cart);

        adminService.insertOrder(order);
        for (LineItem li:order.getLineItems()
             ) {
            li.setOrderId(order.getOrderId());
            adminService.insertLineItem(li);
            adminService.updateInventory(li);
        }

        session.setAttribute("order",order);
        return "order/ViewOrder";
    }

    //用户查看历史订单
    @GetMapping("/listOrder")
    public String listOrder(Model model,  HttpServletRequest req){
        HttpSession session = req.getSession();
        String username = ((Account)session.getAttribute("account")).getUsername();

        List<Order> orderList = adminService.getOrdersByUsername(username);

        model.addAttribute("orderList",orderList);

        return "order/ListOrder";
    }


}
